<?php
namespace  app\common\controller\pay;

use think\Controller;
use app\api\model\wanlshop\PayOutTrade;
use think\Log;
class Julaicai extends Controller
{
    protected $debug = false;
    protected static $appid;
    protected static $mch;
    protected static $key;
    protected static $url;

    public function __construct()
    {
        if ($this->debug){
            self::$url = "https://pay.cljpay.com/api/pay/unifiedOrder";
            self::$mch = "M1663659251";
            self::$appid = "63296cf419ecf92cd216b3b9";
            self::$key = "MwV1n40jJkeU7php7zvNvU2cdFzwSptLzROQ8lVmpcMrDGk3dgIkikuJkQfgpAVHjoFGRqCYXgWtMpLAITvtuhy4AcGMrbHbYIEaCIzT4RjonR6C0ckMr6brB5kSFlpJ";
        }else{
            self::$url = "https://pay.cljpay.com/api/pay/unifiedOrder";
//            self::$mch = "M1702863206";
//            self::$appid = "657fa166e4b08c9a5f1635d7";
//            self::$key = "crSGMrxuXtG7nUnvUAFDPBG9PxeV2EamZvIm4lVrVj04SK5NLKjPJNFSJiqrvOSvXa3mhAqBMje2ofHvUO45SwbGeUm0Fh85qv3y8Ny1XWiXT9qqK5bFxc17sShSUII6";
//
//            self::$mch = "M1703054845";
//            self::$appid = "65828dfde4b08c9a5f1635dd";
//            self::$key = "WPmWr1c6QRB8qfDQCbt9G9GbrcUQ0SI1tGf52zLfGeZKSw84wiBCgmli1R1osdteFy7VtdenykeW5DJKbOQx45UaQFv9FAWt3nk18MvEcDLoJVVPP3YEoBPQ0doLG2iL";

            self::$mch = "M1703483108";
            self::$appid = "658916e4e4b08c9a5f1635ea";
            self::$key = "yNqEQrqG9HnVhmpau4d4f2zRXvTDG7SEmjeXGC4OejUY5irdU92JzlID3NZzbcdgRSrQlm6y9Gpv1a2B9fA0ddgWRy4Bmqly6mRcf3u4V2HwRKeeSS26eRMUlvomr3nZ";

        }
    }

    public function index($param)
    {
        extract($param);
        $payOutTrade = PayOutTrade::where('out_trade_no',$mchOrderNo)->find();
        $good = model('app\api\model\wanlshop\OrderGoods')::where('order_id',$payOutTrade->order_id)->find();
        $subject = $good->title??'商品标题';
        $body = $good->title??'商品描述';
        $notifyUrl = "https://xinweidu.enjoysix.com/api/wanlshop/callback/cailaiju_notify";
        $reqTime = time() * 1000;
        // 支付测试
        $params = [
            'mchNo' => self::$mch,
            'appId' => self::$appid,
            'mchOrderNo' => $mchOrderNo,
            'wayCode' => "QR_CASHIER", //小程序支付
            'amount' => $amount*100,
//            'amount' => 1,
            'currency' => 'cny',
            'subject' => $subject,
            'body' => $body,
            'notifyUrl' => $notifyUrl,
            'reqTime' => $reqTime,
            'version' => '1.0',
        ];
//        if($params['wayCode'] == 'WX_LITE'){

//        $channelExtra = [
//            'openid'=>$openid
//        ];
//        $params['channelExtra'] = json_encode($channelExtra);
//        }
        $channelExtra = [
            'payDataType'=>'codeUrl',
            'entryPageType'=>'lite'
        ];
        $params['channelExtra'] = json_encode($channelExtra);
        $params['signType']= "MD5";
        $params['sign']= self::sign($params);

        Log::error("支付参数");
        Log::error($params);

        $result = \fast\Http::post(self::$url, $params);
        Log::error("支付结果");
        Log::error($result);
        return json_decode($result);
    }

    public function h5($param)
    {
        self::$mch = "M1703123189";
        self::$appid = "658398f5e4b08c9a5f1635df";
        self::$key = "nU12P9LealrB2A1UrUuPut7SC23vAJWP20fSyxX5jwWWvRAlJ33Ktkisb5yDuWMeY6VQN5rcCS2gdPCKJeajHZrTVhOTl90xRI7aN8CIxff9jiooI3Rct6Kz5OMiWGZh";
        extract($param);
        $payOutTrade = PayOutTrade::where('out_trade_no',$mchOrderNo)->find();
        $good = model('app\api\model\wanlshop\OrderGoods')::where('order_id',$payOutTrade->order_id)->find();
        $subject = $good->title??'商品标题';
        $body = $good->title??'商品描述';
//        $subject = '商品标题';
//        $body = '商品描述';
        $notifyUrl = "https://xinweidu.enjoysix.com/api/wanlshop/callback/cailaiju_notify";
        $reqTime = time() * 1000;
        // 支付测试
        $params = [
            'mchNo' => self::$mch,
            'appId' => self::$appid,
            'mchOrderNo' => $mchOrderNo,
            'wayCode' => "QR_CASHIER",
            'amount' => $amount*100,
//            'amount' => 100,
            'currency' => 'cny',
            'subject' => $subject,
            'body' => $body,
            'notifyUrl' => $notifyUrl,
//            'returnUrl' => 'https://xinweidu.lifeepay.com/h5e/#/pages/product/detail?id='.$good->goods_id,
            'returnUrl' => 'https://xinweidu.lifeepay.com/h5test/#/pages/product/detail?id='.$good->goods_id,
            'reqTime' => $reqTime,
            'version' => '1.0',
        ];
//        if($params['wayCode'] == 'WX_LITE'){
//        $channelExtra = [
//            'openid'=>$openid
//        ];
//        $params['channelExtra'] = json_encode($channelExtra);
//        }
        $channelExtra = [
//            'payDataType'=>'codeUrl',
//            'entryPageType'=>'h5'
            'payDataType'=>'payurl',
            'entryPageType'=>'lite',
            'entryLiteType'=>'wxh5'
        ];
        $params['channelExtra'] = json_encode($channelExtra);
        $params['signType']= "MD5";
        $params['sign']= self::sign($params);
        $result = \fast\Http::post(self::$url, $params);
        return json_decode($result);
    }

    protected static function sign($params)
    {
        ksort($params);
        $stringA = '';
        foreach ($params as $key => $value) {
            if ($value !== '' && $key !== 'sign') {
                $stringA .= $key . '=' . $value . '&';
            }
        }
        $stringA = rtrim($stringA, '&');
        $stringSignTemp = $stringA . '&key=' . self::$key;
        $str = md5($stringSignTemp);
        return strtoupper($str);
    }
}